package com.kintreda.ieco_server.module.account.web;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import com.kintreda.ieco_server.bean.account.Menu;
import com.kintreda.ieco_server.bean.account.Role;
import com.kintreda.ieco_server.core.web.BaseController;
import com.kintreda.ieco_server.core.web.R;
import com.kintreda.ieco_server.module.account.service.IMenuService;
import com.kintreda.ieco_server.module.account.service.IRoleService;
import com.kintreda.ieco_server.module.account.service.IUserService;
import com.kintreda.ieco_server.util.page.PageBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/***
 * 角色Controller
 */
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {
	@Resource
	private IUserService userService;
	@Resource
	private IRoleService roleService;
	@Resource
	private IMenuService menuService;
	
	@RequestMapping(value = "/query",method=RequestMethod.GET)
	public String query() {
		return "account/role/role-list";
	}

	@RequestMapping(value = "/query",method=RequestMethod.POST)
	@ResponseBody
	public Object query(
			@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "15") Integer rows,
			Role role) {

		ModelAndView mv = new ModelAndView("account/role/role-list");

		PageBean<Role> pageObj = new PageBean<Role>();
		pageObj.setPage(page);
		pageObj.setRows(rows);
		pageObj = roleService.query(pageObj, role);

		mv.addObject("page", pageObj);

		return pageObj;
	}

	@RequestMapping(value = "/add", method = RequestMethod.GET)
	public  ModelAndView add() {
    ModelAndView mv = new ModelAndView("account/role/role-add");
		mv.addObject("user",getUser());
		List<Menu> Menus = menuService.getAll();
		mv.addObject("Menus", Menus);
		return mv;
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public Object add(Role role,String[] mensId) {

		try {
			Role validateRole = this.roleService.getRoleByCode(role.getNameEn());

			if(validateRole != null) {
				return R.error("该角色标识已存请不要重复添加");
			}


			if(mensId != null) {
				Set<Menu> menus = new HashSet<Menu>();
				for (int i = 0; i < mensId.length; i++) {
					Menu menu = menuService.get(mensId[i]);
					menus.add(menu);
					//递归父级节点
					recursionMenus(menus, menu);
				}
				role.setMenus(menus);
			}
			roleService.save(role);
			return R.ok("信息添加成功");
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("添加角色发生错误:" + e.getMessage());
		}
	}

	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public ModelAndView edit(@PathVariable("id") String id) {

		ModelAndView mv = new ModelAndView("account/role/role-edit");

		Role role = roleService.get(id);

		mv.addObject("role", role);

		return mv;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Object edit(Role role,String[] mensId) {
		
		try {
			
			Role roleDB = roleService.get(role.getId());
			roleDB.setNameEn(role.getNameEn());
			roleDB.setNameZh(role.getNameZh());
			roleDB.setStatus(role.getStatus());
			
			Set<Menu> menus = new HashSet<Menu>();
			
			if(mensId != null) {
				for (int i = 0; i < mensId.length; i++) {
					Menu menu = menuService.get(mensId[i]);
					menus.add(menu);
					//递归父级节点
					recursionMenus(menus, menu);
				}
			}
			
			roleDB.setMenus(menus);

			roleService.update(roleDB);
			
			return R.ok("信息更新成功");
			
		} catch (Exception e) {
			e.printStackTrace();
			return R.error("修改角色发生错误:" + e.getMessage());
		}
	}
	
	
	/***************************************************
	 * 递归菜单，如果选择子节点的，则把父节点也添加进去
	 * @param menus
	 * @param menu
	 */
	private void recursionMenus(Set<Menu> menus,Menu menu) {
		
		if(!"0".equals(menu.getPid())) {
			Menu pMenu = this.menuService.get(menu.getPid());
			if(pMenu != null) {
				menus.add(pMenu);
				this.recursionMenus(menus, pMenu);
			}
		}
	}


	@RequestMapping(value = "/enable", method = RequestMethod.POST)
	@ResponseBody
	public Object enable(String id, String status) {
		try {
			Role roleDB = roleService.get(id);
			roleDB.setStatus(status);
			roleService.update(roleDB);
			return setSuccess();
		} catch (Exception e) {
			e.printStackTrace();
			return setError("修改角色状态发生错误:" + e.getMessage());
		}
	}

}
